package com.hellpro.lolbox.home.weight;


import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.BaseFragment;
import com.hellpro.lolbox.bean.ZXTT;
import com.hellpro.lolbox.config.UrlConfigs;
import com.hellpro.lolbox.home.HomeJsonUtils;
import com.hellpro.lolbox.home.model.OnZXListListener;
import com.hellpro.lolbox.home.presenter.ZXCommListAdapter;
import com.hellpro.lolbox.home.presenter.ZXFragment;
import com.hellpro.lolbox.home.view.IZXCommAFragment;
import com.hellpro.lolbox.utils.ToastShowCenter;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class ZXCommAFragment extends BaseFragment implements IZXCommAFragment {
    private String mPath = UrlConfigs.ZXTOUTIAO;
    private  int page = 1;
    private int pageSize;
    @BindView(R.id.zx_commona)
    PullToRefreshListView zxCommona;
    private ZXCommListAdapter zxCommListAdapter;




    @Override
    public void initView() {
        zxCommona.setMode(PullToRefreshBase.Mode.BOTH);
      zxCommona.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
          @Override
          public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

              String path = mPath+ 1;
                        goDown(path);
              zxCommona.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      if (zxCommona!=null) {
                          zxCommona.onRefreshComplete();
                      }
                  }
              },7000);
          }

          @Override
          public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

              page ++;
              if (page>pageSize){
                 Toast.makeText(getFActivity(),"已到最后一页",Toast.LENGTH_SHORT).show();
                  return;
              }
              String path = mPath+ page;
              goDown(path);
              zxCommona.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      if (zxCommona!=null) {
                          zxCommona.onRefreshComplete();
                          ToastShowCenter.centerToast(getFActivity(),"数据超屌的 ");
                      }
                  }
              },7000);
          }
      });

        zxCommListAdapter = new ZXCommListAdapter();

        zxCommona.setAdapter(zxCommListAdapter);

        String path = mPath + 1;
        goDown(path);



        }





    @Override
    public int getLayoutId() {
        return R.layout.fragment_zxcomm_a;
    }




    @Override
    public void hideProgress() {
        zxCommona.onRefreshComplete();
    }
private Handler mHandler = new Handler(){



    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what ==1){
            Object obj = msg.obj;
            ZXTT zxtt= (ZXTT) obj;
            pageSize = zxtt.getPageSize();
            if (zxCommona!=null) {
                zxCommona.onRefreshComplete();
            }
            zxCommListAdapter.setZXData(obj,0);
        }
    }
};


    //开启下载
    private void goDown(String path){
       new ZXFragment(getFActivity(), path, new OnZXListListener() {
            @Override
            public void loadSuccees(String json) {
                ZXTT data = HomeJsonUtils.getData(json, ZXTT.class);
                Message obtain = Message.obtain();
                obtain.what=1;
                obtain.obj= data;
                mHandler.sendMessage(obtain);

            }

            @Override
            public void loadFail() {

            }
        });

    }
}





